import logging
from coin.exchange.kraken.kr_rest.currency import KrakenCurrency
from coin.strategy.accounting.pnl_balance_printer.pnl_balance_printer import FuturesPnlBalancePrinter
from coin.strategy.accounting.pnl_balance_printer.util import calculate_unrealized_pnl


class KrakenFuturesPnlBalancePrinter(FuturesPnlBalancePrinter):
  def __init__(self, products, bookmap, reserve_map, logger=None):
    self._currency = KrakenCurrency.FromStrCurrency('USD')
    super().__init__(products,
                     bookmap,
                     reserve_map,
                     logger=(logger or logging.getLogger('KrakenFuturesPnlBalancePrinter')))

  def _get_pos(self, product):
    book = self._bookmap.get_book(product)
    if book is None or not book.has_ask() or not book.has_bid():
      return None, None

    pos = self._og.get_position(product)
    if product.quote.currency == 'BTC':
      self._logger.error('Pnl printer for %s not implemented!' % product)
      return pos, 0

    average_entry_price = self._og.get_average_entry_price(product)
    unrealized_pnl = calculate_unrealized_pnl(product,
                                              pos,
                                              average_entry_price,
                                              book.ask0().price,
                                              book.bid0().price)
    return pos, unrealized_pnl
